package cn.edu.nit.sys.service.impl;

import cn.edu.nit.sys.entity.Department;
import cn.edu.nit.sys.mapper.DepartmentMapper;
import cn.edu.nit.sys.service.DepartmentService;
import cn.edu.nit.sys.utils.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaobai
 * @Date 2022/8/27 21:55
 * @Version 1.0.0
 */
@Service
@Slf4j
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public R getDepartmentList() {
        List<Department> departmentList = baseMapper.getDepartmentList();
        List<Department> rs = new ArrayList<>();
        for(Department parent : departmentList) {
            Department department = new Department();
            List<Department> childrenList = new ArrayList<>();
            String[] majorIds = parent.getMajorId().split(",");
            String[] majors = parent.getMajor().split(",");
            department.setId(parent.getId());
            department.setName(parent.getDept());
            department.setLabel(parent.getDept());
            department.setDeptId(parent.getDeptId());
            department.setDept(parent.getDept());
            for(int i = 0; i < majorIds.length; i++) {
                Department children = new Department();
                children.setId(majorIds[i]);
                children.setName(majors[i]);
                children.setLabel(majors[i]);
                children.setMajorId(majorIds[i]);
                children.setMajor(majors[i]);
                childrenList.add(children);
            }
            department.setChildren(childrenList);
            rs.add(department);
        }
        log.info("处理结果：{}", rs);
        return R.ok(rs);
    }
}
